﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_ClientContacts : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        handleEditMode();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (btnSave.CommandArgument == "")
        {
            tbl_client_contact_details model = new tbl_client_contact_details();
            model.USER_CODE = ddlUSER_CODE.SelectedValue;
            model.CONTACT_NAME = txtCONTACT_NAME.Text.Trim();
            model.PRIMARY_CONTACT_NUMBER = txtPRIMARY_CONTACT_NUMBER.Text.Trim();
            model.SECONDARY_CONTACT_NUMBER = txtSECONDARY_CONTACT_NUMBER.Text.Trim();
            model.DEPARTMENT_ID = ddlDEPARTMENT_ID.SelectedValue;
            model.DESIGNATION_ID = ddlDESIGNATION_ID.SelectedValue;
            model.IS_ACTIVE = Convert.ToBoolean(chkIS_ACTIVE.Checked);
            model.CREATED_BY = User.Identity.Name;
            model.CREATED_DATE = DateTime.Now;



            BLL_tbl_client_contact_details bll = new BLL_tbl_client_contact_details();
            bll.insert(model);
        }
        else
        {
            tbl_client_contact_details model = new tbl_client_contact_details();
            model.CONTACT_ID = Convert.ToInt32(btnSave.CommandArgument);
            model.USER_CODE = ddlUSER_CODE.SelectedValue;
            model.CONTACT_NAME = txtCONTACT_NAME.Text.Trim();
            model.PRIMARY_CONTACT_NUMBER = txtPRIMARY_CONTACT_NUMBER.Text.Trim();
            model.SECONDARY_CONTACT_NUMBER = txtSECONDARY_CONTACT_NUMBER.Text.Trim();
            model.DEPARTMENT_ID = ddlDEPARTMENT_ID.SelectedValue;
            model.DESIGNATION_ID = ddlDESIGNATION_ID.SelectedValue;
            model.IS_ACTIVE = Convert.ToBoolean(chkIS_ACTIVE.Checked);
            model.UPDATED_BY = User.Identity.Name;
            model.UPDATED_DATE = DateTime.Now;



            BLL_tbl_client_contact_details bll = new BLL_tbl_client_contact_details();
            bll.update(model);
            btnSave.CommandArgument = "";
            Response.Redirect(Request.Url.AbsolutePath);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        //ddlUSER_CODE.SelectedIndex = -1;
        //txtCONTACT_NAME.Text = "";
        //txtPRIMARY_CONTACT_NUMBER.Text = "";
        //txtSECONDARY_CONTACT_NUMBER.Text = "";
        //ddlDEPARTMENT_ID.SelectedIndex = -1;
        //ddlDESIGNATION_ID.SelectedIndex = -1;
        //chkIS_ACTIVE.Checked = false;
        //txtCREATED_BY.Text = "";
        //txtCREATED_DATE.Text = "";

        //btnSave.CommandArgument = "";

        Response.Redirect(Request.Url.AbsolutePath);
    }

    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        tbl_client_contact_details model = new tbl_client_contact_details();
        model.CONTACT_ID = Convert.ToInt32(id); ;

        BLL_tbl_client_contact_details bll = new BLL_tbl_client_contact_details();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }


    protected void handleEditMode()
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["editRecord"] != null)
            {
                string id = Request.QueryString["editRecord"];
                BLL_tbl_client_contact_details bll = new BLL_tbl_client_contact_details();
                List<tbl_client_contact_details> list = bll.select(Convert.ToInt32(id));
                foreach (tbl_client_contact_details tbl in list)
                {
                    btnSave.CommandArgument = tbl.CONTACT_ID.ToString();
                    ddlUSER_CODE.SelectedValue = tbl.USER_CODE;
                    txtCONTACT_NAME.Text = tbl.CONTACT_NAME;
                    txtPRIMARY_CONTACT_NUMBER.Text = tbl.PRIMARY_CONTACT_NUMBER;
                    txtSECONDARY_CONTACT_NUMBER.Text = tbl.SECONDARY_CONTACT_NUMBER;
                    ddlDEPARTMENT_ID.SelectedValue = tbl.DEPARTMENT_ID;
                    ddlDESIGNATION_ID.SelectedValue = tbl.DESIGNATION_ID;
                    chkIS_ACTIVE.Checked = tbl.IS_ACTIVE;
                }
            }
        }
    }


                
}